package Client;

import static java.lang.System.exit;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import rmip.ClientInt;
import rmip.ServerInt;


public class ClientImp extends UnicastRemoteObject implements ClientInt{
      
      static  public Hashtable<String, Conversation > chatingNow=new Hashtable<String ,Conversation> ();
      static public Hashtable<String,gconver> groupchatNow=new Hashtable<String, gconver>();
      ChatWin userWindow;
  ClientImp() throws RemoteException{
    
  }


   public static void main( String [] args)
   {
        
      
   }

    @Override
    public void sendMessageToClient(String sender, String msg) throws RemoteException {
           chatingNow.get(sender).jTextArea1.append(msg);
    }
    public void sendGMessageToInterActArea(String sender,String msg) throws RemoteException
    {
        chatingNow.get(sender).jTextField1.setText(msg);
    }

    public void setuserWindow(ChatWin ch)
    {
        this.userWindow=ch;
    }
    @Override
    public void convGui(String senderMail, String receverMail, ServerInt s) throws RemoteException {
     if(!(this.chatingNow.containsKey(receverMail)))
     {
          Conversation ch=new Conversation(senderMail, receverMail,s);
          ch.setTitle(receverMail);
          chatingNow.put(receverMail,ch);
          ch.setVisible(true);
     }
         
    }
      @Override
      public void repaintList() throws RemoteException{
          userWindow.repaintList();
      }    
    @Override
    public boolean inconv(String mail) throws RemoteException {
            return chatingNow.containsKey(mail);
           }

    @Override
    public void sendGMessageToClient(String chatname, String msg) throws RemoteException {
       groupchatNow.get(chatname).chatArea.append(msg);
       
    }

    @Override
    public void startNewGroupChat(String chatname, String mail, ServerInt s) throws RemoteException {
    if(!(groupchatNow.containsKey(chatname)))
    {
     gconver ch=new gconver(mail,chatname, s,this);
     ch.setVisible(true);
  
     groupchatNow.put(chatname, ch);
     }
    }

    @Override
    public void resetGroup(String chatname,Vector g) throws RemoteException {
         groupchatNow.get(chatname).resetGroup(g);
    }
    public void removeFromHashmap(String chatname)
    {
        groupchatNow.remove(chatname);
    }
   
public void repaintGroupChat(String chatname,Vector<String> g)throws RemoteException{
     if(groupchatNow.containsKey(chatname))
    {
        groupchatNow.get(chatname).repaintGroupChat(g);
    }
    }   

    @Override
    public void showRequest(String myMail,String fMail, String msg,ServerInt si) throws RemoteException {
        String message = "You have new friend request from "+myMail+"\nHe said : "+msg+"\n Do you want to add him to your friend list?";
        int choice = JOptionPane.showConfirmDialog(null ,message ,"Friend request", JOptionPane.INFORMATION_MESSAGE, JOptionPane.YES_NO_OPTION);
        if(choice==0)
            si.confirmRequest(myMail,fMail,1);
        if(choice==1)
            si.confirmRequest(myMail,fMail,0);
    }
       
}
